﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace auxiliary_proj
{
    public partial class FormSentMessage : Form
    {
        private static MySqlConnection DBCon;
        FormViewSentMsg vsm;

        public FormSentMessage()
        {
            InitializeComponent();
            //Cell double click event handler
            this.dataGridView1.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentDoubleClick);
            //get connection
            Program.parseServerInfo();
            DBCon = new MySqlConnection(Program.getConnection());
            qSentMsg(Employee.User);
        }
        //Query that get all the sent messages 
        private void qSentMsg(string receive_emp)
        {
            try
            {
                string query = "SELECT CONCAT( u.first_name,  ' ', u.last_name ) AS 'To', m.subject AS 'Subject', m.timestamp AS 'Date', m.receive_user_id, m.message AS 'message'" +
                                "FROM MESSAGING as m, USER as u " +
                                "WHERE send_user_id= \"" + receive_emp + "\" AND u.user_name = m.receive_user_id ORDER BY TIMESTAMP DESC";
                try
                {
                    //bind data to grid
                    dataGridView1.DataSource = Query.select(query);
                }
                catch (MySqlException exc) { MessageBox.Show(exc.Message); }

            }
            catch (DataException ex) { MessageBox.Show(ex.Message); }
            finally
            {
                DBCon.Close();
            }
        }
        //Event handler for cell double click 
        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            RichTextBox rtb = new RichTextBox();
            rtb.Rtf = dataGridView1.Rows[e.RowIndex].Cells["message"].Value.ToString();
            vsm = new FormViewSentMsg((string)dataGridView1.Rows[e.RowIndex].Cells["To"].Value, (string)dataGridView1.Rows[e.RowIndex].Cells["Subject"].Value, dataGridView1.Rows[e.RowIndex].Cells["Date"].Value.ToString(), rtb.Rtf);
            vsm.Show();
        }
    }
}
